;(function($){
	
	$.dragStop = {
			drag : function (v) {
				if($.dragStop.obj.type=="r"){
					var width,height,differWidth=$.dragStop.obj.E.width(),differHeight=$.dragStop.obj.E.height();
					if(typeof $.dragStop.obj.before =="function"){
						$.dragStop.obj.before();
					}
					if($.dragStop.obj.resizeWidth){				
						width=Math.min(Math.max(v.pageX - $.dragStop.obj.M.pX + $.dragStop.obj.M.W, $.dragStop.obj.minWidth),$.dragStop.obj.maxWidth);
						differWidth=width-differWidth;
						$.dragStop.obj.E.css({
							width : width
						});
					}
					if($.dragStop.obj.resizeHeight){
						height=Math.min(Math.max(v.pageY -$.dragStop.obj.M.pY + $.dragStop.obj.M.H, $.dragStop.obj.minHeight),$.dragStop.obj.maxHeight);
						differHeight=height-differHeight;
						$.dragStop.obj.E.css({
						height : height
						});
					}
					if(typeof $.dragStop.obj.after =="function"){
						$.dragStop.obj.after(width,height,differWidth,differHeight);
					}
				}else{
					if(typeof $.dragStop.obj.before =="function"){
						$.dragStop.obj.before();
					}
					var winWidth,winHeight;
					winWidth=$(window).width();
					winHeight=$(window).height();					
					var scrollTop=$(document).scrollTop();
					var scrollLeft=$(document).scrollLeft();
					var left=$.dragStop.obj.M.X + v.pageX - $.dragStop.obj.M.pX,top=$.dragStop.obj.M.Y + v.pageY - $.dragStop.obj.M.pY;
					if($.dragStop.obj.inWin){
						if(left<scrollLeft){
							left=scrollLeft;
						}else if(left>scrollLeft+winWidth-$.dragStop.obj.M.W){
							left=scrollLeft+winWidth-$.dragStop.obj.M.W;
						}
						if(top<scrollTop){
							top=scrollTop;
						}else if(top>scrollTop+winHeight-$.dragStop.obj.M.H){
							top=scrollTop+winHeight-$.dragStop.obj.M.H;
						}
					}
					$.dragStop.obj.E.css({
						left :left,
						top :top
					});
					if(typeof $.dragStop.obj.after =="function"){
						$.dragStop.obj.after(width,height,differWidth,differHeight);
					}
				}
				return false;			
			},
			stop : function () {
				$.dragStop.obj.E.css('opacity', 1);
				$(document).unbind('mousemove', $.dragStop.drag).unbind('mouseup', $.dragStop.stop);
				if(typeof $.dragStop.obj.stop == "function"){
					$.dragStop.obj.stop();
				}
			},
			obj:{}
		};
		/*
			拖动改变大小
			editor:longjia
		*/
		$.fn.cgResize = function(o) {
			var o=$.extend(true,{},{
				handler:".handler",
				maxWidth:1000,
				maxHeight:1000,
				minWidth:250,
				minHeight:100,
				resizeWidth:true,
				resizeHeight:true,
				opacity:0.5,
				zIndex:$.maxZindex,
				before:function(){
				},
				after:function(){
				},
				start:function(){
					
				},
				stop:function(){
									
				}
			},o);
			var init = function (e) {//e表示对象，h拖动或者改变大小的节点，k拖动或者改变大小
				return e.each(function () {
					var h = (o.handler) ? $(o.handler, $(this)) : $(this);//获取拖动或者改变大小对象，如果为空的话，则为整个对话框				
					h.bind('mousedown', {
						e : $(this)//对话框对象
					}, function (v) {
						if(v.which!=1) return;
						if(typeof o.start == "function"){
							o.start();
						}
						var d = v.data,//获取传过来的数据对象，e和k
						p = {};
						o.E = d.e;//E为对话框对象
						if (o.E.css('position') != 'relative') {
							try {
								o.E.position(p);
							} catch (e) {}					
						}
						o.M = {
							X : p.left || f('left') || 0,//获取left值
							Y : p.top || f('top') || 0,//获取top值
							W : o.E.outerWidth(true)|| o.E[0].scrollWidth || 0,//获取对话框的宽度
							H : o.E.outerHeight(true) || o.E[0].scrollHeight || 0,//获取对话框的高度
							pX : v.pageX,//获取鼠标的左边距
							pY : v.pageY,//获取鼠标上边距
							o : o.E.css('opacity')//获取对话框的透明度
						};
						if(o.zIndex){
							o.zIndex=++$.maxZindex;
						}
						o.E.css({
							opacity : o.opacity,
							"z-index":o.zIndex
						});
						o.type="r"
						$.dragStop.obj=o;
						$(document).mousemove($.dragStop.drag).mouseup($.dragStop.stop).mouseleave($.dragStop.stop);				
						v.preventDefault();
					});				
					var f = function (k) {return parseInt(o.E.css(k)) || false;};
				});
			};
			return init(this);
		};
		/*
			拖动窗体
			editor:longjia
		*/
		$.fn.cgDrag=function(o){
			 var o=$.extend(true,{},{
				handler:".title",
				inWin:true,
				opacity:0.5,
				before:function(){
				},
				after:function(){
				},
				start:function(){
					
				},
				stop:function(){
					
				}
			},o);
			var init = function (e) {//e表示对象，h拖动或者改变大小的节点，k拖动或者改变大小
				return e.each(function () {
					var h = (o.handler) ? $(o.handler, $(this)) : $(this);//获取拖动或者改变大小对象，如果为空的话，则为整个对话框
					h.bind('mousedown', {
						e :  $(this)//对话框对象
					}, function (v) {
						if(v.which!=1) return;
						if(typeof o.start == "function"){
							o.start();
						}
						var d = v.data,//获取传过来的数据对象，e和k
						p = {};
						o.E = d.e;//E为对话框对象
						if (o.E.css('position') != 'relative') {
							try {
								p=o.E.position();
							} catch (e) {}
						}
						o.M = {
							X : p.left || f("left") || 0,//获取left值
							Y : p.top || f("top") || 0,//获取top值
							W : o.E.outerWidth(true) || o.E[0].scrollWidth || 0,//获取对话框的宽度
							H : o.E.outerHeight(true) || o.E[0].scrollHeight || 0,//获取对话框的高度
							pX : v.pageX,//获取鼠标的左边距
							pY : v.pageY,//获取鼠标上边距
							o : o.E.css('opacity')//获取对话框的透明度
						};
						o.E.css({
							"opacity" : o.opacity
						});
						o.type="d"
						$.dragStop.obj=o;
						$(document).mousemove($.dragStop.drag).mouseup($.dragStop.stop).mouseleave($.dragStop.stop);
						v.preventDefault();
					});
				});
			};
			var f = function (k) {return parseInt(o.E.css(k)) || false;};
			return init(this);
		};
		
		$.fn.tips=function(o){
			o=$.extend(true,{
				moveHandler:".tips-top",
				resizeHandler:"",
				closeHandler:"#close",
				reloadHandler:"#reload",
				width:250,
				height:240,
				auto:100000, //页面自动刷新时间
				position:{top:null,right:0,bottom:0,left:null},
				resizeAfter:null,
				dragBefore:null,
				html:'<div class="event-tips" id="event-tips"><div class="tips-top"><div class="top-left"></div><div class="top-title"><div>系统提醒您有以下事务待处理</div></div>'+
				'<div class="top-right"></div></div><div class="tips-main"><div class="tolls"><div class="loading-msg"></div>'+
				'<input type="button" id="close" value=""/><input type="button" id="reload" value=""/>'+
				'</div><div class="tips-data"><table cellspacing="0" border="0" cellpadding="0" width="100%">'+
				'<tr align="center" style="font-size:12px;"><th width="90%">事务名称</th><th>条数</th></tr>'+
				'</table></div><div class="handler"></div></div></div>',
				success:null,
				error:null,
				beforeSend:null,
				close:null,
				type:"get",
				url:"",
				data:null,
				dataType:"text"			
			},o||{});
			var $this,$dataBox,$msg,$reload,$close;
			var setPosition=function($this){
				var setIE6=function(width,height,left,top){
					var obj={};
					if(o.position.left||o.position.left==0){
						obj.left=left+o.position.left;
					}
					if(o.position.right||o.position.right==0){
						obj.left=left+width-o.position.right-$this.outerWidth();
					}
					if(o.position.top||o.position.top==0){
						obj.top=top+o.position.top;
					}
					if(o.position.bottom||o.position.bottom==0){						
						obj.top=top+height-o.position.bottom-$this.outerHeight();
					}
					return obj;
				}
				if($.browser.msie&&$.browser.version<7){//如果浏览器为ie6则处理
					var winWidth=$(window).width(),
					winHeight=$(window).height(),
					scrollLeft=$(window).scrollLeft(),
					scrollTop=$(window).scrollTop();
					$this.css("position","absolute");
					var p=setIE6(winWidth,winHeight,scrollLeft,scrollTop);
					$this.css({
						"left":p.left,
						"top":p.top
					});					
					$(window).bind("scroll resize",function(){	
						winWidth=$(window).width(),
						winHeight=$(window).height(),
						scrollLeft=$(window).scrollLeft(),
						scrollTop=$(window).scrollTop();
						p=setIE6(winWidth,winHeight,scrollLeft,scrollTop);						
						$this.stop(true).animate({
							"left":p.left,
							"top":p.top
						},200);
					});
				}else{
					$this.css({"position":"fixed","left":o.position.left,"top":o.position.top,"right":o.position.right,"bottom":o.position.bottom});
				}
			};
			var ajax=function(){
				$.ajax({
					type:o.type,
					dataType:o.dataType,
					success:o.success,
					error:o.error,
					beforeSend:o.beforeSend,
					url:o.url,
					data:o.data
				});
			};
			var init=function(){
				$this=$(o.html);
				$dataBox=$(".tips-data table",$this);
				$msg=$(".loading-msg",$this);
				$reload=$(o.reloadHandler,$this);
				$close=$(o.closeHandler,$this);				
				$this.appendTo("body");	
				$this.css({"height":o.height,"width":o.width});
				$(".top-title",$this).css("width",o.width-15);
				$(".tips-data",$this).css("height",o.height-60);
				setPosition($this);
				ajax();
				setInterval(function(){
					ajax();
				},o.auto);
				$reload.click(function(){
					ajax();
				});
				$close.click(o.close);
				$this.cgResize({
					after:o.resizeAfter,
					opacity:1
				}).cgDrag({
					handler:o.moveHandler,
					before:o.dragBefore,
					opacity:1,
					after:function(){						
						if(!$.browser.msie||$.browser.version>=7){
							o.position.left=$this.offset().left-$(window).scrollLeft();
							o.position.top=$this.offset().top-$(window).scrollTop();							
							$this.css({
								"top":o.position.top,
								"left":o.position.left,
								"position":"fixed"
							});
						}else{
							if(o.position.left||o.position.left==0) o.position.left=$this.offset().left-$(window).scrollLeft();
							if(o.position.top||o.position.top==0) o.position.top=$this.offset().top-$(window).scrollTop();
							if(o.position.right||o.position.right==0) o.position.right=$(window).width()-($this.offset().left-$(window).scrollLeft())-$this.outerWidth();
							if(o.position.bottom||o.position.bottom==0) o.position.bottom=$(window).height()-($this.offset().top-$(window).scrollTop())-$this.outerHeight();				
						}
					}
				});
			}
			init();
			
			return $this;
		};
})(jQuery)


$(function(){
		function getCookie(name) {
		var arr,
		reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
		if (arr = document.cookie.match(reg))
			return (arr[2]);
		else
			return null;
	}
	function setCookie(name,value,time){			
		var exp = new Date(); 
		exp.setTime(exp.getTime() + time);
		document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString()+";path=/";
	}
	function delCookie(name){
		document.cookie = name+"=;expires="+(new Date(0)).toGMTString()+";path=/";
	}
		var $this=$.fn.tips({
			error:function(){
				//alert("NO");
				$(".loading-msg",$this).html("加载失败，请重试！").css("background","url(../Img/no.png) no-repeat left center").show();
			},
			beforeSend:function(){
				$(".loading-msg",$this).html("正在加载中……").css("background","url(../Img/loading.gif) no-repeat left center").show();
			},	
			success:function(data){
				//alert(data);
				$(".tips-data table tr",$this).not($(".tips-data table tr:eq(0)",$this)).remove();				
				$(data).appendTo($(".tips-data table",$this));
				$(".loading-msg",$this).hide();
			},
			close:function(){
				if(confirm("退出系统前不再提醒（暂时关闭自动提醒）？")){
					alert("已关闭自动提醒，如需提醒请点击左下方”事件提醒“按钮");
					setCookie("isClose",1,30*60*1000);
					$this.hide();
				}
			},
			width:300,   //设置提示框的宽度
			url:"TiXingServlet?flag="+Math.random(),
			resizeAfter:function(width,height,differWidth,differHeight){
				$(".top-title",$this).width("+="+differWidth);
				$(".tips-data",$this).height("+="+differHeight);
			},
			dragBefore:function(){				
				$this.css({
					"top":$this.offset().top,
					"left":$this.offset().left,
					"right":"auto",
					"bottom":"auto",
					"position":"absolute"
				})
			}
		});	
		$this.hide();
		var flag=getCookie("isClose");
		if(!flag||flag==0){
			$this.show();		
		}
		$("#opentips").click(function(){
			delCookie("isClose");
			$this.show();							  
		});
	});
